package cn.com.KnowNews.servlet;

import cn.com.KnowNews.entity.Admin;
import cn.com.KnowNews.service.imlp.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddAdminServlet",urlPatterns = "/AddAdmin")
public class AddAdminServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.setCharacterEncoding("utf-8");
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        String username = request.getParameter("addusername");
        String password = request.getParameter("addpassword1");
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        AdminServiceImpl adminService = new AdminServiceImpl();
        try {
          if(adminService.AddAdmin(admin)==true){
              response.getWriter().print("<script>alert('注册成功！')</script>");
              response.setHeader("refresh","1;url="+basePath+"/admin/showAdminadd.jsp");
          }else{
              response.getWriter().print("<script>alert('注册失败！')</script>");
          }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
